<?php
class ForumsController extends AppController {
	public $name = 'Forums';

	function beforeFilter() {
		$this->Auth->allow('displayforums');
		$this->Auth->authorize = 'controller';
		$this->Security->blackHoleCallback = 'blackhole';
	}

	function blackhole($type) {
		if($this->request->params['action'] == 'updatePositions'){
			return true;
		}
		throw new MethodNotAllowedException(__('Nope: ' . $type . ' - ' . Router::url($this->here, true)));
	}

	public function isAuthorized($user) {
		if (in_array(strtolower($this->action), array('add', 'edit', 'delete', 'index', 'updatepositions'))) {
			return (AuthComponent::User('role') == '3') ? true : false;
		}
		return true;
	}

	public function index(){
		$this->set('forums', $this->Forum->find('all', array('order' => 'position ASC')));
	}

	public function add(){
		if($this->request->is('post') || $this->request->is('put')){
			$this->Forum->create();
			if($this->Forum->Save($this->request->data)){
				$this->Session->setFlash('Forum created', 'default', array('class' => 'success'));
				$this->redirect(array('controller' => 'forums', 'action' => 'index'));
			}
		}
	}

	public function edit($id = null){
		$this->Forum->id = $id;
		if(!$this->Forum->exists()){
			throw new NotFoundException('Forum not found');
		}
		if($this->request->is('get')){
			$this->request->data = $this->Forum->read();
		}else{
			if($this->Forum->save($this->request->data)){
				$this->Session->setFlash('Forum updated', 'default', array('class' => 'success'));
				$this->redirect(array('controller' => 'forums', 'action' => 'index'));
			}
		}
	}

	public function displayforums($id = null, $slug = null){
		$this->set('title_for_layout', SITE_NAME . ' &bull; Forums');
		$this->Forum->Behaviors->attach('Containable');
		$this->set('forums', $this->Forum->find('all',
			array(
				'order' => 'position ASC',
				'contain' => array(
					'SubForum' => array(
						'order' => 'position ASC',
						'fields' => array('name', 'description', 'id', 'thread_count', 'topic_count'),
						'Thread' => array(
							'User' => array(
								'fields' => array('username')
							),
							'LatestUser' => array(
								'fields' => array('username')
							),
							'fields' => array('title', 'body', 'thread_id', 'id', 'user_id', 'latest_reply_thread_id', 'latest_reply_created'),
							'order' => array('Thread.latest_reply_created DESC'),
							'limit' => 1
		))))));
	}

	public function updatePositions(){
		if($this->request->is('ajax')){
			$this->autoRender = false;
			foreach($_POST['forum'] as $key=>$val){
				$this->Forum->id = $val;
				$this->Forum->set('position', $key);
				$this->Forum->save();
			}

		}
	}

	public function delete(){
		if($this->request->is('get')){
			throw new MethodNotAllowedException();
		}elseif($this->request->is('post')){
			$i = 0;
			foreach($this->request->data['Forum']['selectedForums'] AS $forumId){
				// delete forum
				$this->Forum->id = $forumId;
				$this->Forum->delete();
				$i++;
			}
			$this->Session->setFlash($i . ': Forums have been deleted.', 'default', array('class' => 'success'));
			$this->redirect(array('action' => 'index'));
		}
	}
}